# 原始方案太麻烦了，很多用户不会用，已经废弃


import os
import json
import random
import string
from datetime import datetime

# 定义生成随机字符串的函数
def generate_random_id(length, format_type):
    if format_type == "uuid":
        # 生成类似 UUID 的随机字符串（8-4-4-4-12）
        parts = [
            ''.join(random.choices(string.hexdigits.upper(), k=8)),
            ''.join(random.choices(string.hexdigits.upper(), k=4)),
            ''.join(random.choices(string.hexdigits.upper(), k=4)),
            ''.join(random.choices(string.hexdigits.upper(), k=4)),
            ''.join(random.choices(string.hexdigits.upper(), k=12))
        ]
        return '-'.join(parts)
    elif format_type == "hex":
        # 生成类似十六进制的随机字符串
        return ''.join(random.choices(string.hexdigits.lower(), k=length))
    else:
        # 生成包含数字、英文大小写的随机字符串
        chars = string.ascii_letters + string.digits
        return ''.join(random.choice(chars) for _ in range(length))

# 定义修改设备 ID 的函数
def modify_device_ids(file_path):
    # 读取 JSON 文件
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)

   # 修改设备 ID 的函数
def modify_device_ids(file_path):
    # 读取 JSON 文件，使用 utf-8-sig 编码
    with open(file_path, 'r', encoding='utf-8-sig') as file:  # 修改这里
        data = json.load(file)

    # 修改设备 ID
    if "telemetry.sqmId" in data:
        data["telemetry.sqmId"] = "{" + generate_random_id(36, "uuid") + "}"  # 类似 {E3DB74FD-D35A-430D-9D0C-0AFF91442298}
    if "telemetry.macMachineId" in data:
        data["telemetry.macMachineId"] = generate_random_id(36, "uuid")  # 类似 9b8e269d-bbe7-4095-bdb2-8b09be49bfdb
    if "telemetry.machineId" in data:
        data["telemetry.machineId"] = generate_random_id(64, "hex")  # 类似 d7769fd3f9534d4c8112490c21402c87a1a11e90466146a997e5468db7809741
    if "telemetry.devDeviceId" in data:
        data["telemetry.devDeviceId"] = generate_random_id(36, "uuid")  # 类似 a965ba4a-f591-4634-8187-1ad2d94c1928

    # 将修改后的数据写回文件
    with open(file_path, 'w', encoding='utf-8') as file:
        json.dump(data, file, indent=4)

    # 打印修改后的设备 ID
    print("更改后的设备 ID 内容：")
    if "telemetry.sqmId" in data:
        print(f'telemetry.sqmId: {data["telemetry.sqmId"]}')
    if "telemetry.macMachineId" in data:
        print(f'telemetry.macMachineId: {data["telemetry.macMachineId"]}')
    if "telemetry.machineId" in data:
        print(f'telemetry.machineId: {data["telemetry.machineId"]}')
    if "telemetry.devDeviceId" in data:
        print(f'telemetry.devDeviceId: {data["telemetry.devDeviceId"]}')

    print("设备 ID 已成功修改！")

# 定义创建备份文件的函数
def create_backup_file(backup_dir):
    # 生成文件名（例如：MachineGuid_20250205_115616.txt）
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    file_name = f"MachineGuid_{timestamp}.txt"
    file_path = os.path.join(backup_dir, file_name)

    # 生成随机内容（类似 05d2138d-3388-4911-aa48-5e3c5add0077）
    content = generate_random_id(36, "uuid")

    # 写入文件
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(content)

    print(f"备份文件已创建：{file_path}")
    print(f"文件内容：{content}")

# 主程序
if __name__ == "__main__":
    # 用户输入
    user_input = input("请选择模式（输入 1: Win 模式, 2: macOS 模式, 3: Linux 模式）: ")

    if user_input == "1":
        # Win 模式
        print("Win 修改 ID 模式")
        appdata_dir = os.getenv('APPDATA')
        if not appdata_dir:
            print("无法获取 %APPDATA% 目录路径！")
            exit(1)
        file_path = os.path.join(appdata_dir, "Cursor", "User", "globalStorage", "storage.json")
        backup_dir = os.path.join(appdata_dir, "MachineGuid_Backups")
        if not os.path.exists(backup_dir):
            os.makedirs(backup_dir)
        create_backup_file(backup_dir)
    elif user_input == "2":
        # macOS 模式
        print("macOS 修改 ID 模式")
        home_dir = os.getenv('HOME')
        if not home_dir:
            print("无法获取用户主目录路径！")
            exit(1)
        file_path = os.path.join(home_dir, "Library", "Application Support", "Cursor", "User", "globalStorage", "storage.json")
    elif user_input == "3":
        # Linux 模式
        print("Linux 修改 ID 模式")
        home_dir = os.getenv('HOME')
        if not home_dir:
            print("无法获取用户主目录路径！")
            exit(1)
        file_path = os.path.join(home_dir, ".config", "Cursor", "User", "globalStorage", "storage.json")
    else:
        print("输入无效，操作已取消。")
        exit(1)

    # 检查文件是否存在
    if not os.path.exists(file_path):
        print(f"文件 {file_path} 不存在！")
        exit(1)

    # 执行修改操作
    modify_device_ids(file_path)
